package com.size.mall.mallsearch.Controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.size.mall.config.annotations.LoginRequired;
import com.size.mall.mallapi.entity.*;
import com.size.mall.mallapi.service.PmsBaseAttrInfoService;
import com.size.mall.mallapi.service.SearchService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
public class IndexController {


    @Reference
    SearchService searchService;

    @Reference
    private PmsBaseAttrInfoService attrInfoService;

    @GetMapping("list.html")
    public String list(PmsSearchParam param, ModelMap model) {

        List<PmsSearchSkuInfo> infos = searchService.ls(param);

        model.put("skuLsInfoList", infos);

        // 抽取包含的平台属性集合
        Set<String> valueIds = CollUtil.newHashSet();
        // stream 流抽取
        infos.stream().map(info -> info.getPmsSkuAttrValueList()).map(pmsSkuAttrValues -> {
            return pmsSkuAttrValues.stream().map(
                    // 抽取不重复的valueId
                    pmsSkuAttrValue -> {
                        String valueId = pmsSkuAttrValue.getValueId();
                        valueIds.add(valueId);
                        return valueId;


                    }).collect(Collectors.toSet());
        }).collect(Collectors.toSet());


        // 转出列表
        List<PmsBaseAttrInfo> pmsBaseAttrInfos = attrInfoService.getAttrInfosByValueIds(valueIds);
        model.put("attrList", pmsBaseAttrInfos);


        // 删除多余的列表 // 迭代器经典应用场景:删除当前组
        String[] delValueIds = param.getValueId();

        if (ArrayUtil.isNotEmpty(delValueIds)) {
            List<PmsSearchCrumb> crumbs = new ArrayList<>();
            for (String delValueId : delValueIds) {
                Iterator<PmsBaseAttrInfo> iterator = pmsBaseAttrInfos.iterator();
                PmsSearchCrumb crumb = new PmsSearchCrumb();
                // 生成面包屑的参数
                crumb.setValueId(delValueId);
                crumb.setUrlParam(_getUrlParamForCrumb(param, delValueId));

                while (iterator.hasNext()) {
                    PmsBaseAttrInfo next = iterator.next();

                    List<PmsBaseAttrValue> attrValueList = next.getAttrValueList();

                    for (PmsBaseAttrValue pmsBaseAttrValue : attrValueList) {
                        String valueId = pmsBaseAttrValue.getId();
                        if (delValueId.equals(valueId)) {
                            crumb.setValueName(pmsBaseAttrValue.getValueName());


                            iterator.remove();
                        }


                    }


                }
                crumbs.add(crumb);
            }

            model.put("attrValueSelectedList", crumbs);
        }

        String urlParam = _getUrlParam(param);
        model.put("urlParam", urlParam);

        String keyword = param.getKeyword();
        if (StrUtil.isNotBlank(keyword)) {
            model.put("keyword", keyword);
        }

        return "list";

    }


    /***
     *  辅助方法,拼接 参数地址
     * @param param
     * @return
     */
    private String _getUrlParamForCrumb(PmsSearchParam param, String delValueId) {

        String paramUrl = "";

        String keyword = param.getKeyword();
        String catalog3Id = param.getCatalog3Id();
        String[] valueIds = param.getValueId();

        // 拼接关键字
        if (StrUtil.isNotBlank(keyword)) {
            if (StrUtil.isNotBlank(paramUrl)) {
                paramUrl += "&";
            }

            paramUrl += "keyword=" + keyword;
        }


        // 拼接三级分类id
        if (StrUtil.isNotBlank(catalog3Id)) {
            if (StrUtil.isNotBlank(paramUrl)) {
                paramUrl += "&";
            }
            paramUrl += "catalog3Id=" + catalog3Id;


        }

        // 拼接所有valueId
        if (null != valueIds && valueIds.length != 0) {
            for (String valueId : valueIds) {

                if (!valueId.equals(delValueId)) {
                    paramUrl += "&valueId=" + valueId;
                }
            }

        }


        return paramUrl;

    }


    /***
     *  辅助方法,拼接 参数地址
     * @param param
     * @return
     */
    private String _getUrlParam(PmsSearchParam param) {

        String paramUrl = "";

        String keyword = param.getKeyword();
        String catalog3Id = param.getCatalog3Id();
        String[] valueIds = param.getValueId();

        // 拼接关键字
        if (StrUtil.isNotBlank(keyword)) {
            if (StrUtil.isNotBlank(paramUrl)) {
                paramUrl += "&";
            }

            paramUrl += "keyword=" + keyword;
        }


        // 拼接三级分类id
        if (StrUtil.isNotBlank(catalog3Id)) {
            if (StrUtil.isNotBlank(paramUrl)) {
                paramUrl += "&";
            }
            paramUrl += "catalog3Id=" + catalog3Id;


        }

        // 拼接所有valueId
        if (null != valueIds && valueIds.length != 0) {
            for (String valueId : valueIds) {

                paramUrl += "&valueId=" + valueId;
            }

        }


        return paramUrl;

    }


    @GetMapping("/index")
    @LoginRequired(loginSuccess = false)
    public String getIndex() {

        return "index";
    }
}
